package com.wxmp.wxapi.job;

import cn.hutool.core.collection.CollectionUtil;
import com.wxmp.wxapi.service.impl.PushFactory;
import com.wxmp.wxcms.domain.*;
import com.wxmp.wxcms.mapper.AccountFansDao;
import com.wxmp.wxcms.mapper.TplMsgTextDao;
import com.wxmp.wxcms.mapper.WeatherChinaDao;
import com.wxmp.wxcms.service.AccountService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zzm
 * @Date 2022/8/12 10:32
 * @Version 1.0
 **/
@Component
public class SendTextTask {

    private final Logger logger  = LoggerFactory.getLogger(getClass());

    @Autowired
    private AccountFansDao accountFansDao;
    @Autowired
    private TplMsgTextDao tplMsgTextDao;

    @Autowired
    private AccountService accountService;

    @Autowired
    private WeatherChinaDao weatherChinaDao;


    @Autowired
    private PushFactory pushFactory;


    /**
     * 每天凌晨1点执行
     * @throws ParseException
     */
    @Scheduled(cron = "0 0 8 1/1 * ?")
    public void push(){
        logger.info("-----早上定时推送开始----");
        //获取所有粉丝openid
        List<AccountFans> list = accountFansDao.list();
        if (CollectionUtil.isNotEmpty(list)) {
            for (AccountFans a : list) {
                try {
                    if (isFilter(a.getStatus())) continue;
                    logger.info("早上定时推送粉丝:公众号:{},昵称:{},openId:{}",a.getAccount(),a.getNickName(),a.getOpenId());
                    TplMsgText text = new TplMsgText();
                    text.setAccount(a.getAccount());
                    text.setInputcode("早上");
                    List<TplMsgText> textList = tplMsgTextDao.getTplMsgTextList(text);
                    Account account = accountService.getByAccount(a.getAccount());
                    //获取地区编码
                    String districtCode = weatherChinaDao.selectByDistrict(a.getDistrict());
                    //默认北京市
                    String district = "110100";
                    if (StringUtils.isNotBlank(districtCode)) {
                        district = districtCode;
                    }
                    a.setDistrict(district);
                    if (CollectionUtil.isNotEmpty(textList)) {
                        pushFactory.push("morning",a,textList.get(0).getTplId(),account);
                    }
                }catch (Exception e){
                    logger.error("定时推送失败=>",e);
                }
            }
        }


    }
    @Scheduled(cron = "0 30 23 * * ?")
    public void nightPush(){
        logger.info("-----晚上定时推送开始----");
        //获取所有粉丝openid
        List<AccountFans> list = accountFansDao.list();
        if (CollectionUtil.isNotEmpty(list)) {
            for (AccountFans a : list) {
                if (isFilter(a.getStatus())) continue;
                logger.info("晚上定时推送粉丝:公众号:{},昵称:{},openId:{}",a.getAccount(),a.getNickName(),a.getOpenId());
                TplMsgText text = new TplMsgText();
                text.setAccount(a.getAccount());
                text.setInputcode("晚上");
                List<TplMsgText> textList = tplMsgTextDao.getTplMsgTextList(text);
                Account account = accountService.getByAccount(a.getAccount());
                if (CollectionUtil.isNotEmpty(textList)) {
                    pushFactory.push("night",a,textList.get(0).getTplId(),account);
                }
            }
        }
    }

    @Scheduled(cron = "0 30 11 * * ? ")
    public void noonPush(){
        logger.info("-----中午定时推送开始----");
        //获取所有粉丝openid
        List<AccountFans> list = accountFansDao.list();
        if (CollectionUtil.isNotEmpty(list)) {
            for (AccountFans a : list) {
                if (isFilter(a.getStatus())) continue;
                logger.info("中午定时推送粉丝:公众号:{},昵称:{},openId:{}",a.getAccount(),a.getNickName(),a.getOpenId());
                TplMsgText text = new TplMsgText();
                text.setAccount(a.getAccount());
                text.setInputcode("中午");
                List<TplMsgText> textList = tplMsgTextDao.getTplMsgTextList(text);
                Account account = accountService.getByAccount(a.getAccount());
                if (CollectionUtil.isNotEmpty(textList)) {
                    pushFactory.push("noon",a,textList.get(0).getTplId(),account);
                }
            }
        }
    }


    @Scheduled(cron = "0 0 10,15 1/1 * ?  ")
    public void loafPush(){
        logger.info("-----摸鱼办定时推送开始----");
        //获取所有粉丝openid
        List<AccountFans> list = accountFansDao.list();
        if (CollectionUtil.isNotEmpty(list)) {
            for (AccountFans a : list) {
                logger.info("摸鱼办定时推送粉丝:公众号:{},昵称:{},openId:{}",a.getAccount(),a.getNickName(),a.getOpenId());
                TplMsgText text = new TplMsgText();
                text.setAccount(a.getAccount());
                text.setInputcode("摸鱼");
                List<TplMsgText> textList = tplMsgTextDao.getTplMsgTextList(text);
                Account account = accountService.getByAccount(a.getAccount());
                if (CollectionUtil.isNotEmpty(textList)) {
                    pushFactory.push("loaf",a,textList.get(0).getTplId(),account);
                }
            }
        }
    }

    @Scheduled(cron = "0 5 8 1/1 * ? ")
    public void warmReminderPush(){
        logger.info("-----温馨提醒定时推送开始----");
        //获取所有粉丝openid
        List<AccountFans> list = accountFansDao.list();
        if (CollectionUtil.isNotEmpty(list)) {
            for (AccountFans a : list) {
                if (isFilter(a.getStatus())) continue;
                logger.info("温馨提醒定时推送粉丝:公众号:{},昵称:{},openId:{}",a.getAccount(),a.getNickName(),a.getOpenId());
                TplMsgText text = new TplMsgText();
                text.setAccount(a.getAccount());
                text.setInputcode("温馨提醒");
                List<TplMsgText> textList = tplMsgTextDao.getTplMsgTextList(text);
                Account account = accountService.getByAccount(a.getAccount());
                if (CollectionUtil.isNotEmpty(textList)) {
                    pushFactory.push("warmReminder",a,textList.get(0).getTplId(),account);
                }
            }
        }
    }

    private Boolean isFilter(Integer status) {
        return status == 0;
    }

}
